package cn.xerllent.security.security.vo;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.stream.Collectors;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class JwtUser implements UserDetails {

    private static final long serialVersionUID = 1L;

	private final Long id;
	private final String corpId;
	private final String userId;
	private final String name; //第三方不给真实名name=userId
//    private final String username; //企业微信按照UserDetails接口就是username=corpId+"$"+userId
	private final String alias;
    private final String gender;
    private final String avatar;
	private final Integer mainDepartmentId;
	private final String mail;
    private final String telephone;

    @JsonIgnore
    private final Collection<SimpleGrantedAuthority> authorities;

    private final boolean enabled;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private final LocalDateTime lastlogtime;
    
    /**
     * 企业微信按照UserDetails接口就是username=corpId+"$"+userId
     * */
    @JsonIgnore    //表示不输出到前端
    @Override
    public String getUsername() {
    	return corpId+"$"+userId;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @JsonIgnore
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return null;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

    public Collection<String> getPermissions() {
        return authorities.stream().map(GrantedAuthority::getAuthority).collect(Collectors.toSet());
    }
}
